home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 32 / solsys.zip / SOLSYS.C < prev    next >
Text File  |  1989-07-23  |  5KB  |  231 lines

  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. #define G 6.67e-3
  7. #define MAXNUM 45
  8.  
  9. struct object {
  10.     double rx,ry,vx,vy,mass;
  11.     } masses[MAXNUM];
  12.  
  13. struct accel {
  14.     double ax,ay;
  15.     };
  16.  
  17. int xmax,ymax,maxcolor,NUM;
  18. double xscale,yscale;
  19.  
  20. /********************************************************************/
  21. void graphicson(void)
  22.     {
  23.     int err,graphdriver = DETECT, graphmode;
  24.  
  25.     initgraph(&graphdriver,&graphmode,"");
  26.     if ((err=graphresult()) != 0)
  27.         {
  28.         printf("%s\n",grapherrormsg(err));
  29.         abort();
  30.         }
  31.  
  32.     maxcolor = getmaxcolor();
  33.     xmax = getmaxx();
  34.     ymax = getmaxy();
  35.     xscale = 16384.0 / xmax;
  36.     yscale = 16384.0 / ymax;
  37.     }
  38. /********************************************************************/
  39.  
  40. void init(void)
  41.     {
  42.     int i;
  43.     double r,v;
  44.  
  45.     randomize();
  46.     masses[0].rx = 0;    masses[0].ry = 0;
  47.     masses[0].vx = 0;    masses[0].vy = 0;
  48.     masses[0].mass = 700000000;
  49.     for (i=1; i<NUM; i++)
  50.         {
  51.         masses[i].rx = (rand()-16384)/10.0;
  52.         masses[i].ry = (rand()-16384)/10.0;
  53.         r = sqrt((*(masses+i)).rx * (*(masses+i)).rx +
  54.                  (*(masses+i)).ry * (*(masses+i)).ry);
  55.         masses[i].mass = rand() * 100.0;
  56.         v = sqrt(G * (*masses).mass / r);
  57.         masses[i].vx = masses[i].ry / r * v;
  58.         masses[i].vy = -masses[i].rx / r * v;
  59.         }
  60.     }
  61.  
  62. /*********************************************************************/
  63.  
  64. void getaccel(int masso,struct accel *acc)
  65.     {
  66.     int i;
  67.     double ax,ay,ar,r2,dx,dy;
  68.  
  69.     ax = 0;        ay = 0;
  70.     for (i=0; i<NUM; i++)
  71.         {
  72.         if (i != masso)
  73.             {
  74.             dx = (*(masses+i)).rx - (*(masses+masso)).rx;
  75.             dy = (*(masses+i)).ry - (*(masses+masso)).ry;
  76.             r2 = dx*dx + dy*dy;
  77.             ar = (*(masses+i)).mass / (r2 * sqrt(r2));
  78.             ax += ar * dx;
  79.             ay += ar * dy;
  80.             }
  81.         }
  82.     (*acc).ax = ax * G;
  83.     (*acc).ay = ay * G;
  84.     }
  85.  
  86. /*********************************************************************/
  87.  
  88. void plotmass(int i)
  89.     {
  90.     double xpos,ypos;
  91.  
  92.     xpos = ((*(masses+i)).rx + 8192) / xscale;
  93.     ypos = ((*(masses+i)).ry + 8192) / yscale;
  94.     if ((xpos>0)&&(xpos<xmax)&&(ypos>0)&&(ypos<ymax))
  95.         putpixel(xpos,ypos,maxcolor);
  96.     }
  97.  
  98. /*********************************************************************/
  99.  
  100. void starplot(int i)
  101.     {
  102.     double xpos,ypos;
  103.     int color;
  104.  
  105.     xpos = ((*(masses+i)).rx + 8192) / xscale;
  106.     ypos = ((*(masses+i)).ry + 8192) / yscale;
  107.     if ((xpos>1)&&(xpos<xmax)&&(ypos>1)&&(ypos<ymax))
  108.         {
  109.         putpixel(xpos,ypos,maxcolor);
  110.         putpixel(xpos-1,ypos,maxcolor);
  111.         putpixel(xpos+1,ypos,maxcolor);
  112.         putpixel(xpos,ypos-1,maxcolor);
  113.         putpixel(xpos,ypos+1,maxcolor);
  114.         }
  115.     }
  116.  
  117. /*********************************************************************/
  118.  
  119. void printmass(int i)
  120.     {
  121.     double dx,dy,d;
  122.     char t[20];
  123.     int j;
  124.  
  125.     if (!i)    {
  126.             dx = (*masses).rx;
  127.             dy = (*masses).ry;
  128.             }
  129.     else    {
  130.             dx = (*(masses+i)).rx-(*masses).rx;
  131.             dy = (*(masses+i)).ry-(*masses).ry;
  132.             }
  133.     d = sqrt(dx*dx+dy*dy);
  134.     if (d<10000)
  135.         {
  136.         for (j=0; j<4; j++) t[j]=0;
  137.         itoa(i,t,10);
  138.         for (j=0; j<4; j++) if (!t[j]) t[j]=' ';
  139.         itoa(d,t+4,10);
  140.         t[3] = ':';
  141.         if (!i) { t[0]='S'; t[1]='U'; t[2]='N';
  142.                   outtextxy(0,0,t); }
  143.         else outtextxy((i-1)/10*100,((i-1)%10)*10+10,t);
  144.         }
  145.     }
  146.  
  147. /*********************************************************************/
  148.  
  149. void opscreen(void)
  150.     {
  151.     char s[30];
  152.  
  153.     clrscr();
  154.     printf("\t\t\t    SOLAR SYSTEM SIMULATOR\n\n\n");
  155.     printf("\tThis program will create and simulate a solar system\n");
  156.     printf("\tfor you.  You tell it how many planets you would like, up\n");
  157.     printf("\tto a maximum of 40, and it will give each planet a position,\n");
  158.     printf("\tmass, and velocity randomly.  It will then calculate and\n");
  159.     printf("\tdisplay the motions of each planet, along with their\n");
  160.     printf("\tdistances from the sun.  Also displayed is the distance the\n");
  161.     printf("\tsun has moved from it's point of origin.  The calculations\n");
  162.     printf("\tare all based on newton's laws, which means that each planet\n");
  163.     printf("\t(and the sun) is affected by the gravitational pull of every\n");
  164.     printf("\tother planet in the solar system.  This produces a rather\n");
  165.     printf("\tcomplex and unpredictable display.\n\n");
  166.     printf("\t\tYou can press [ENTER] to turn tracking on and off,\n");
  167.     printf("\t\tpress the space bar to end the display.\n\n\n");
  168.     printf("How many planets would you like to see?\n");
  169.     do {
  170.         gets(s);
  171.         NUM = atoi(s);
  172.         }
  173.     while ((NUM<1)||(NUM>40));
  174.     NUM++;
  175.     }
  176.  
  177. /*********************************************************************/
  178.  
  179. main()
  180.     {
  181.     int i,s=0,track=0,ch;
  182.     struct accel acc;
  183.  
  184.     opscreen();
  185.     init();
  186.     graphicson();
  187.     setfillstyle(SOLID_FILL,BLACK);
  188.     do
  189.         {
  190.         if (kbhit()) ch=getch();
  191.             else ch='';
  192.         if (ch==13) {    track=1-track;    setactivepage(s);    cleardevice();    }
  193.         if (!track)
  194.             {
  195.             setactivepage(s);
  196.             setvisualpage(1-s);
  197.             cleardevice();
  198.             s = 1-s;
  199.             }
  200.  
  201.     /* Plot positions */
  202.  
  203.         if (track) bar(0,0,NUM/10*100+100,110);
  204.         starplot(0);
  205.         printmass(0);
  206.         for (i=1; i<NUM; i++)
  207.             {
  208.             plotmass(i);
  209.             printmass(i);
  210.             }
  211.  
  212.     /* Calculate new velocities */
  213.  
  214.         for (i=0; i<NUM; i++)
  215.             {
  216.             getaccel(i,&acc);
  217.             (*(masses+i)).vx += acc.ax;
  218.             (*(masses+i)).vy += acc.ay;
  219.             }
  220.  
  221.     /* Calculate new positions */
  222.  
  223.         for (i=0; i<NUM; i++)
  224.             {
  225.             (*(masses+i)).rx += (*(masses+i)).vx;
  226.             (*(masses+i)).ry += (*(masses+i)).vy;
  227.             }
  228.         }
  229.     while (ch != ' ');
  230.     closegraph();
  231.     }